package com.ruizhong.PIW.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.PIW.dao.UserMapper;
import com.ruizhong.PIW.entity.TUser;
import com.ruizhong.PIW.service.UserService;
import com.ruizhong.PIW.vo.PageSearchVO;
import com.ruizhong.PIW.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TUser> implements UserService {

    @Override
    public void addUser(TUser user) {
        if (StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPersonImage())){
            throw new RuntimeException("缺少必要信息，请填写完整");
        }
        boolean save = save(user);
        if (!save){
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    public PageVO<TUser> getUserInfoPage(PageSearchVO pageSearchVO) {
        Integer pageNo = pageSearchVO.getPageNo();
        Integer pageSize = pageSearchVO.getPageSize();
        String inputOfSearch = pageSearchVO.getInputOfSearch();
        Page<TUser> page = new Page(pageNo, pageSize);
        Page<TUser> tUserPage = lambdaQuery()
                .like(!StringUtils.isEmpty(inputOfSearch),TUser::getName, inputOfSearch)
                .page(page);
        PageVO<TUser> tUserPageVO = new PageVO<>(page.getTotal(), tUserPage.getRecords());
        return tUserPageVO;
    }


}
